package com.faxsun.web.processor;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.payment.service.OrderToPaymentRequestDTOService;
import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.element.AbstractLocalVariableDefinitionElementProcessor;
import org.thymeleaf.standard.expression.Expression;
import org.thymeleaf.standard.expression.StandardExpressions;

public class FSOrderHistoryCheckoutProcessor extends AbstractLocalVariableDefinitionElementProcessor {

	@Resource(name = "blOrderToPaymentRequestDTOService")
	protected OrderToPaymentRequestDTOService orderToPaymentRequestDTOService;

	protected FSOrderHistoryCheckoutProcessor() {
		super("order_history_checkout");
	}

	@Override
	protected Map<String, Object> getNewLocalVariables(Arguments arguments, Element element) {
		Map<String, Object> localVars = new HashMap<String, Object>();

		Expression expression = (Expression) StandardExpressions.getExpressionParser(
				arguments.getConfiguration()).parseExpression(arguments.getConfiguration(), arguments,
				element.getAttributeValue("order_repay"));
		Order order = (Order) expression.execute(arguments.getConfiguration(), arguments);

		localVars.put("paymentRequestDTO", orderToPaymentRequestDTOService.translateOrder(order));

		return localVars;
	}

	@Override
	protected boolean removeHostElement(Arguments arguments, Element element) {
		return false;
	}

	@Override
	public int getPrecedence() {
		return 200;
	}

}
